/*
  * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.payment.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.PaymentTermException;
import com.pitop.core.tools.StringTools;
import com.pitop.payment.dao.PaymentTermDAO;
import com.pitop.payment.entity.PaymentTerm;
import com.pitop.payment.entity.PaymentTermState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("paymentTermSV")
@Slf4j
public class PaymentTermSVImpl extends BaseSVImpl<PaymentTerm, Long> implements PaymentTermSV {

    @Autowired
    private PaymentTermDAO paymentTermDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return paymentTermDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(PaymentTerm entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 加载一个对象PaymentTerm
     *
     * @param id * @param code 编码
     * @return PaymentTerm
     */
    @Override
    public PaymentTerm load(Long id, String code) {
        if (id == null && code == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return paymentTermDAO.load(param);
    }

    /**
     * 加载一个对象PaymentTerm 通过id
     *
     * @param id
     * @return PaymentTerm
     */
    @Override
    public PaymentTerm loadById(Long id) {
        if (id == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return paymentTermDAO.loadById(id);
    }

    /**
     * 加载一个对象PaymentTerm 通过code
     *
     * @param code 编码
     * @return PaymentTerm
     */
    @Override
    public PaymentTerm loadByCode(String code) {
        if (code == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return paymentTermDAO.loadByCode(code);
    }

    /**
     * 加载一个对象PaymentTerm 通过payment
     *
     * @param payment 编码
     * @return PaymentTerm
     */
    @Override
    public PaymentTerm loadByPayment(String payment) {
        if (StringTools.isEmpty(payment)) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Map<String, Object> params = new HashedMap();
        params.put("payment", payment);
        return paymentTermDAO.load(params);
    }


    /**
     * 根据主键id,oldStates 共同更新 PaymentTerm 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, PaymentTermState newState, PaymentTermState... oldStates) {
        if (id == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        paymentTermDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 PaymentTerm 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(String code, PaymentTermState newState, PaymentTermState... oldStates) {
        if (code == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        paymentTermDAO.updateStateByCode(code, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 PaymentTerm 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, PaymentTermState state) {
        if (id == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        paymentTermDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键code 更新 PaymentTerm 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(String code, PaymentTermState state) {
        if (code == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        paymentTermDAO.updateByCode(code, state.name(), new Date());
    }


    /**
     * 删除对象PaymentTerm
     *
     * @param id * @param code 编码
     * @return PaymentTerm
     */
    @Override
    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new PaymentTermException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        paymentTermDAO.delete(param);
    }


    /**
     * 查询PaymentTerm分页
     *
     * @param paymentTerm 对象
     * @param offset      查询开始行
     * @param limit       查询行数
     * @return List<PaymentTerm>
     */
    @Override
    public List<PaymentTerm> list(PaymentTerm paymentTerm, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (paymentTerm != null) {
            map = JSON.parseObject(JSON.toJSONString(paymentTerm, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return paymentTermDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(PaymentTerm paymentTerm) {
        Map<String, Object> map = null;
        if (paymentTerm != null) {
            map = JSON.parseObject(JSON.toJSONString(paymentTerm, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return paymentTermDAO.count(map);
    }

    /**
     * 查询PaymentTerm分页
     *
     * @param id     * @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<PaymentTerm>
     */
    @Override
    public List<PaymentTerm> list(Long id, String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return paymentTermDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return paymentTermDAO.count(param);
    }

    /**
     * 保存支付方式
     *
     * @param payment 支付方式
     * @return PaymentTerm
     */
    @Override
    public PaymentTerm save(String payment) {
        PaymentTerm paymentTerm = new PaymentTerm();
        paymentTerm.setCode(String.valueOf(uidGenerator.getUID()));
        paymentTerm.setPayment(payment);
        super.save(paymentTerm);
        return paymentTerm;
    }

}
